export const formatSpecList = (originSpecList) => {
  let result = [];
  const fun = (spec) => {
    return spec?.forEach((sku) => {
      result.push({
        id: sku.id,
        title: sku.title,
      });
      if (sku?.attrList) {
        fun(sku?.attrList);
      }
    });
  };
  fun(originSpecList);
  return result;
};

export const handleAssembleSku = (spec, skus) => {
  const newSku = skus?.map((sku) => {
    sku.specAttrsObj = {};
    sku.specAttrsArr = [];
    sku.specAttrs.map((attrId, attrIndex) => {
      if (attrId) {
        //如果有值
        const tempParent = spec[attrIndex];
        if (tempParent) {
          let newAttr = {
            attr_key: "",
            attr_value: "",
          };
          const attrObj = spec[attrIndex].attrList.filter(
            (item) => item.id === attrId
          )[0];
          if (attrObj) {
            sku.specAttrsObj[tempParent.title] = attrObj.title;
            newAttr.attr_key = tempParent.title;
            newAttr.attr_value = attrObj.title;
            sku.specAttrsArr.push(newAttr);
          }
        }
      }
    });
    return sku;
  });
  return newSku;
};
